/*
 * Copyright (c) Huawei Device Co., Ltd. 2023. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "session.h"

#include <gtest/gtest.h>
#include <memory>

#include "channel.h"
#include "ipc_object_stub.h"
#include "loghelper.h"
#include "reader.h"
#include "se_service.h"
#include "service_manager.h"

namespace OHOS {
namespace SE {
namespace TEST {
using namespace testing::ext;
using namespace OHOS::SE::OMAPI;
static const std::string CHANNEL_NUMBER = "A000000151000000";
static bool g_isSeSupported = false;

class SessionTest : public testing::Test {
public:
    static void SetUpTestCase() {};
    static void TearDownTestCase() {};
    void SetUp();
    void TearDown();
public:
    std::shared_ptr<SEService> seService_;
    std::shared_ptr<Reader> reader_;
    std::shared_ptr<Session> session_;
    OHOS::sptr<IRemoteObject> obj;
    SeAgentList terminalTable_;
};

void SessionTest::SetUp()
{
    InfoLog("SessionTest::SetUp begin!");
    seService_ = std::make_shared<SEService>();
    bool initStatus = false;
    seService_->InitSEService(initStatus, nullptr);
    sleep(1);
    EXPECT_TRUE(initStatus);

    vector<std::shared_ptr<Reader>> readerLists;
    seService_->GetReaders(readerLists);
    g_isSeSupported = readerLists.empty();
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    for (auto reader : readerLists) {
        std::string readerName {};
        reader->GetName(readerName);
        if (readerName.compare("eSE") == 0) {
            reader_ = reader;
            break;
        }
    }
    EXPECT_TRUE(reader_);
    if (reader_ != nullptr) {
        reader_->OpenSession(session_);
    }
    EXPECT_TRUE(session_);
    InfoLog("SessionTest::SetUp done!");
}

void SessionTest::TearDown()
{
    InfoLog("SessionTest::TearDown begin!");
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_) {
        session_->Close();
        session_ = nullptr;
    }
    if (reader_) {
        reader_->CloseSessions();
        reader_ = nullptr;
    }
    if (seService_) {
        seService_->Shutdown();
        seService_ = nullptr;
    }
    InfoLog("SessionTest::TearDown done!");
}

/**
 * @tc.name: GetReader_Test001
 * @tc.desc: Test Session GetReader.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, GetReader_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_ != nullptr) {
        std::weak_ptr<Reader> reader;
        int statusCode = session_->GetReader(reader);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_FALSE(reader.expired());
    }
}

/**
 * @tc.name: GetATR_Test001
 * @tc.desc: Test Session GetATR.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, GetATR_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_ != nullptr) {
        std::string atr;
        int statusCode = session_->GetATR(atr);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(atr.length() > 0);
    }
}

/**
 * @tc.name: IsClosed_Test001
 * @tc.desc: Test Session IsClosed.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, IsClosed_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_ != nullptr) {
        bool isClosed = true;
        int statusCode = session_->IsClosed(isClosed);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_FALSE(isClosed);
    }
}

/**
 * @tc.name: OpenBasicChannel_Test001
 * @tc.desc: Test Session OpenBasicChannel.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, OpenBasicChannel_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_ != nullptr) {
        std::shared_ptr<Channel> channel;
        std::string response = "";
        int statusCode = session_->OpenBasicChannel(CHANNEL_NUMBER, 0, channel, response);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(channel);
    }
}

/**
 * @tc.name: OpenBasicChannel_Test002
 * @tc.desc: Test Session OpenBasicChannel.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, OpenBasicChannel_Test002, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_ != nullptr) {
        std::shared_ptr<Channel> channel;
        std::string response = "";
        int statusCode = session_->OpenBasicChannel(CHANNEL_NUMBER, channel, response);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(channel);
    }
}

/**
 * @tc.name: OpenLogicalChannel_Test001
 * @tc.desc: Test Session OpenLogicalChannel.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, OpenLogicalChannel_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_ != nullptr) {
        std::shared_ptr<Channel> channel;
        std::string response = "";
        int statusCode = session_->OpenLogicalChannel(CHANNEL_NUMBER, 0, channel, response);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(channel);
    }
}

/**
 * @tc.name: OpenLogicalChannel_Test002
 * @tc.desc: Test Session OpenLogicalChannel.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, OpenLogicalChannel_Test002, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_ != nullptr) {
        std::shared_ptr<Channel> channel;
        std::string response = "";
        int statusCode = session_->OpenLogicalChannel(CHANNEL_NUMBER, channel, response);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(channel);
    }
}

/**
 * @tc.name: CloseChannels_Test001
 * @tc.desc: Test Session CloseChannels.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, CloseChannels_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_ != nullptr) {
        int statusCode = session_->CloseChannels();
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
    }
}

/**
 * @tc.name: Close_Test001
 * @tc.desc: Test Session Close.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, Close_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (session_ != nullptr) {
        session_->Close();
        bool isClosed = true;
        int statusCode = session_->IsClosed(isClosed);
        EXPECT_TRUE(statusCode == ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(isClosed);
    }
}

/**
 * @tc.name: InnerCheckSessionStatus_Test001
 * @tc.desc: Test Session InnerCheckSessionStatus.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, InnerCheckSessionStatus_Test001, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    sptr<ISecureElementSession> seSession = nullptr;
    std::shared_ptr<Reader> reader = nullptr;
    std::shared_ptr<Session> session = std::make_shared<Session>(service, seSession, reader);
    if (session != nullptr) {
        std::string atr;
        int statusCode = session->GetATR(atr);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(atr.length() == 0);
    }
}
/**
 * @tc.name: Close_Test002
 * @tc.desc: Test Session Close.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, Close_Test002, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    sptr<ISecureElementSession> seSession = nullptr;
    std::shared_ptr<Reader> reader = nullptr;
    std::shared_ptr<Session> session = std::make_shared<Session>(service, seSession, reader);
    if (session != nullptr) {
        int statusCode = session->Close();
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}

/**
 * @tc.name: IsClosed_Test002
 * @tc.desc: Test Session IsClosed.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, IsClosed_Test002, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    sptr<ISecureElementSession> seSession = nullptr;
    std::shared_ptr<Reader> reader = nullptr;
    std::shared_ptr<Session> session = std::make_shared<Session>(service, seSession, reader);
    if (session != nullptr) {
        bool isClosed = true;
        int statusCode = session->IsClosed(isClosed);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
        EXPECT_TRUE(isClosed);
    }
}

/**
 * @tc.name: CloseChannels_Test002
 * @tc.desc: Test Session CloseChannels.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, CloseChannels_Test002, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    sptr<ISecureElementSession> seSession = nullptr;
    std::shared_ptr<Reader> reader = nullptr;
    std::shared_ptr<Session> session = std::make_shared<Session>(service, seSession, reader);
    if (session != nullptr) {
        int statusCode = session->CloseChannels();
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
    }
}

/**
 * @tc.name: OpenLogicalChannel_Test003
 * @tc.desc: Test Session OpenLogicalChannel.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, OpenLogicalChannel_Test003, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    sptr<ISecureElementSession> seSession = nullptr;
    std::shared_ptr<Reader> reader = nullptr;
    std::shared_ptr<Session> session = std::make_shared<Session>(service, seSession, reader);
    if (session != nullptr) {
        std::shared_ptr<Channel> channel;
        std::string response = "";
        int statusCode = session->OpenLogicalChannel(CHANNEL_NUMBER, 0, channel, response);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
        EXPECT_FALSE(channel);
    }
}

/**
 * @tc.name: OpenBasicChannel_Test003
 * @tc.desc: Test Session OpenBasicChannel.
 * @tc.type: FUNC
 */
HWTEST_F(SessionTest, OpenBasicChannel_Test003, TestSize.Level1)
{
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    std::shared_ptr<SEService> service = nullptr;
    sptr<ISecureElementSession> seSession = nullptr;
    std::shared_ptr<Reader> reader = nullptr;
    std::shared_ptr<Session> session = std::make_shared<Session>(service, seSession, reader);
    if (session != nullptr) {
        std::shared_ptr<Channel> channel;
        std::string response = "";
        int statusCode = session->OpenBasicChannel(CHANNEL_NUMBER, 0, channel, response);
        EXPECT_TRUE(statusCode != ErrorCode::SE_SUCCESS);
        EXPECT_FALSE(channel);
    }
}
} // TEST
} // SE
} // OHOS